//
//  UIView+Alert.m
//  BadBoy
//
//  Created by 余勇 on 2017/3/8.
//  Copyright © 2017年 niusaibing. All rights reserved.
//

#import "UIView+Alert.h"
#import <objc/runtime.h>

@interface UIView ()<UIActionSheetDelegate, UIAlertViewDelegate>

@end


@implementation UIView (Alert)

- (void (^)(NSInteger))clickBlock{
    return objc_getAssociatedObject(self, @selector(clickBlock));
}

- (void)setClickBlock:(void (^)(NSInteger))clickBlock{
    objc_setAssociatedObject(self, @selector(clickBlock), clickBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)alertWithTitle:(NSString *)title
               message:(NSString *)message
                  type:(AlertType)type
           cancleTitle:(NSString *)cancleTitle
            otherTitle:(NSArray *)otherTitle
            clickBlock:(void(^)(NSInteger index))clickBlock{
    if (type == AlertTypeAlert) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:nil, nil];
        for (int i = 0; i < otherTitle.count; i++) {
            [alert addButtonWithTitle:otherTitle[i]];
        }
        self.clickBlock = clickBlock;
        [alert show];
    }else{
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:title
                                                           delegate:self
                                                  cancelButtonTitle:cancleTitle
                                             destructiveButtonTitle:nil otherButtonTitles:nil, nil];
        for (int i = 0; i < otherTitle.count; i++) {
            [sheet addButtonWithTitle:otherTitle[i]];
        }
        self.clickBlock = clickBlock;
        [sheet showInView:self];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    self.clickBlock(buttonIndex);
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (self.clickBlock) {
        self.clickBlock(buttonIndex);
    }
    
}

- (void)drawLineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor verticality:(BOOL)verticality
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:self.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame) / 2)];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    if (verticality == NO) {
        [shapeLayer setLineWidth:CGRectGetHeight(self.frame)];
    }else{
        [shapeLayer setLineWidth:CGRectGetWidth(self.frame)];
    }
    
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    if (verticality == NO) {
        CGPathAddLineToPoint(path, NULL,CGRectGetWidth(self.frame), 0);
    }else{
        CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(self.frame));
    }
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [self.layer addSublayer:shapeLayer];
}

@end
